package com.mkh.st.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;

/**
 * json对象转换
 *
 * @author yhj
 */
public class JsonUtils {

	private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
	public static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * json字符串转JsonNode
	 *
	 * @param str
	 * @return
	 */
	public static JsonNode getJsonNode(String str) {
		try {
			if (StringUtils.isEmpty(str)) {
				return null;
			}
			return objectMapper.readTree(str);
		} catch (JsonProcessingException e) {
			logger.error("getJsonNode error{}", str, e);
			return null;
		}
	}

	/**
	 * JsonNode对象转json字符串
	 *
	 * @param jsonNode
	 * @return
	 */
	public static String jsonNodeToString(JsonNode jsonNode) {
		try {
			if (jsonNode == null) {
				return null;
			}
			return objectMapper.writeValueAsString(jsonNode);
		} catch (JsonProcessingException e) {
			logger.error("jsonNodeToString error{}", jsonNode.asText(), e);
			return null;
		}
	}

	public JsonUtils() {
	}

	/**
	 * 对象转json,属性为null的不参与序列化
	 *
	 * @param object
	 * @return
	 */
	public static String toJson(Object object) {
		try {
			return (new ObjectMapper()).setSerializationInclusion(Include.NON_NULL)
					.writeValueAsString(object);
		} catch (JsonProcessingException var2) {
			throw new RuntimeException(var2);
		}
	}

	/**
	 * 对象转json,属性为null的参与序列化
	 *
	 * @param object
	 * @return
	 */
	public static String toJsonWithNull(Object object) {
		try {
			return (new ObjectMapper()).writeValueAsString(object);
		} catch (JsonProcessingException var2) {
			var2.printStackTrace();
			return null;
		}
	}

	/**
	 * json字符串转map
	 *
	 * @param json
	 * @return
	 */
	public static Map writeJsonToMap(String json) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		Map map = null;

		try {
			map = (Map) mapper.readValue(json, Map.class);
			return map;
		} catch (IOException var4) {
			throw new RuntimeException(var4);
		}
	}

	/**
	 * json字符串转自定义对象
	 *
	 * @param json
	 * @param type
	 * @param <E>
	 * @return
	 */
	public static <E> E fromJson(String json, Class<E> type) {
		try {
			return new ObjectMapper()
					.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
					.readValue(json, type);
		} catch (IOException var3) {
			throw new RuntimeException(var3);
		}
	}

	/**
	 * json转泛型对象
	 * @param json json字符串
	 * @param parametrized 主类型
	 * @param parameterClasses 泛型列表
	 * @param <E> 返回对象
	 * @return 返回对象
	 */
	public static <E> E fromJson(String json,Class<?> parametrized, Class<?>... parameterClasses){
		try {
			return new ObjectMapper().readValue(json, getParametricType(parametrized,parameterClasses));
		} catch (JsonProcessingException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取泛型对象的JavaType，
	 * List<MyBean>, 则调用constructParametricType(ArrayList.class,MyBean.class)
	 * Map<String, MyBean>则调用(HashMap.class,String.class, MyBean.class)
	 *
	 * @param parametrized     主类型
	 * @param parameterClasses 泛型列表
	 * @return JavaType
	 */
	private static JavaType getParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
		return new ObjectMapper().getTypeFactory()
				.constructParametricType(parametrized, parameterClasses);
	}
}
